﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Xml;

namespace GameBanChim_Version1
{
    public class StateResult : GameState
    {
        SpriteFont _largeFont;

        SpriteFont _normalFont;

        SpriteFont _smallFont;

        Background _backGroundWin;

        Background _backGroundLose;

        int _winMark;

        string _titleMessage;

        Bird _normalBird;

        Bird _largeBird;

        List<Button> _dsButton;

        int _iCurrentButton;
        int _iButtonReplay;
        int _iButtonSaveGame;

        SoundEffect _sound;

        public override void InnitState(MyGame owner)
        {
            base.InnitState(owner);

            _largeFont = MyGameOwner.Content.Load<SpriteFont>("LargeFont");
            _normalFont = MyGameOwner.Content.Load<SpriteFont>("NormalFont");
            _smallFont = MyGameOwner.Content.Load<SpriteFont>("SmallFont");

            _backGroundWin = (Background)MyGameOwner.BackGroundManager.CreateObject(4);

            _backGroundLose = (Background)MyGameOwner.BackGroundManager.CreateObject(7);

            XmlDocument _doc = new XmlDocument();
            _doc.Load(@"./XML/HowToWin.xml");
            _winMark = int.Parse(_doc.DocumentElement.InnerText);

            _dsButton = new List<Button>();
            _dsButton.Add((Button)MyGameOwner.ButtonManager.CreateObject(4));
            _dsButton[0].IsPlaySound = false;
            _dsButton.Add((Button)MyGameOwner.ButtonManager.CreateObject(5));
            _dsButton[1].IsPlaySound = false;
            // button save game
            _dsButton.Add((Button)MyGameOwner.ButtonManager.CreateObject(7));
            _dsButton[2].IsPlaySound = false;
            // button replay game
            _dsButton.Add((Button)MyGameOwner.ButtonManager.CreateObject(8));
            _dsButton[3].IsPlaySound = false;

            if (GlobalVariables.GamePlayer.Mark < _winMark)
            {
                _iCurrentButton = 0;
                _iButtonReplay = 3;
                _iButtonSaveGame = 2;
                _titleMessage = "YOU LOSE";
                _sound = MyGameOwner.Content.Load<SoundEffect>("Sound/Lose");
            }
            else
            {
                if (GlobalVariables.GamePlayer.Level == 3)
                {
                    _titleMessage = "YOU WIN";
                    _iCurrentButton = 0;
                    _iButtonReplay = 3;
                    _iButtonSaveGame = 2;
                }
                else
                {
                    _iCurrentButton = 1;
                    _iButtonSaveGame = 2;
                    _titleMessage = "YOUR MARK";
                }
                _sound = MyGameOwner.Content.Load<SoundEffect>("Sound/Win");
            }

            _largeBird = (Bird)MyGameOwner.BirdManager.CreateObject(0);
            _normalBird = (Bird)MyGameOwner.BirdManager.CreateObject(1);
            _sound.Play();
        }

        public override void Update(GameTime gameTime)
        {
            _backGroundWin.Update(gameTime);
            _backGroundLose.Update(gameTime);

            _largeBird.Sprite[0].Update(gameTime);
            _normalBird.Sprite[0].Update(gameTime);

            if (_dsButton[_iCurrentButton].isHover())
            {
                // do nothing
            }
            if (_dsButton[_iCurrentButton].isRelease())
            {
                // do nothing
            }
            if (_dsButton[_iCurrentButton].isClicked())
            {
                if (_iCurrentButton == 0)
                {
                    MyGameOwner.MyGameState = new StateMenu();
                    MyGameOwner.MyGameState.InnitState(MyGameOwner);
                }
                else
                {
                    if (GlobalVariables.GamePlayer.Level < 3)
                    {
                        //GlobalVariables.GamePlayer.Level += 1;
                        MyGameOwner.MyGameState = new StateMainGame(GlobalVariables.GamePlayer.Level +1);
                        MyGameOwner.MyGameState.InnitState(MyGameOwner);
                    }
                    else
                    {
                        MyGameOwner.MyGameState = new StateMenu();
                        MyGameOwner.MyGameState.InnitState(MyGameOwner);
                    }
                }
            }
            // xử lý replay game bị thua:
            if (_dsButton[_iButtonReplay].isHover())
            {
            }
            if (_dsButton[_iButtonReplay].isRelease())
            {
            }
            if (_dsButton[_iButtonReplay].isClicked())
            {
                MyGameOwner.MyGameState = new StateMainGame(GlobalVariables.GamePlayer.Level);
                MyGameOwner.MyGameState.InnitState(MyGameOwner);               
            }
            // xử lý save game:
            if (_dsButton[_iButtonSaveGame].isHover())
            {
            }
            if (_dsButton[_iButtonSaveGame].isRelease())
            {
            }
            if (_dsButton[_iButtonSaveGame].isClicked())
            {
                // lay thong tin nguoi choi de luu lai
                int iCurrentLevel = GlobalVariables.GamePlayer.Level;
                // xu ly luu thong tin nguoi choi
                string _xmlInfo = @"./XML/SaveGame.xml";
                XmlDocument _doc = new XmlDocument();
                _doc.Load(_xmlInfo);
                XmlNode _nodeCurrentGame = _doc.DocumentElement.SelectSingleNode("//CurrentGame");
                _nodeCurrentGame.InnerText = iCurrentLevel.ToString();
                _doc.Save(_xmlInfo);

            }

        }

        public override void Draw(GameTime gameTime, SpriteBatch sb)
        {
            if (GlobalVariables.GamePlayer.Mark < _winMark)
            {
                _backGroundLose.Draw(gameTime, sb);
                _dsButton[_iButtonReplay].Draw(gameTime, sb);
            }
            else
            {
                _backGroundWin.Draw(gameTime, sb);
            }

            sb.DrawString(_largeFont, _titleMessage, new Vector2(150, 15), Color.Green);

            sb.DrawString(_normalFont, GlobalVariables.GamePlayer.NormalBirdCount.ToString() + "        X", new Vector2(170, 145), Color.Red);
            sb.Draw(_normalBird.Sprite[0].Texture2D[_normalBird.Sprite[0].Itexture2D], new Vector2(400, 120), Color.White);

            sb.DrawString(_normalFont, GlobalVariables.GamePlayer.LargeBirdCount.ToString() + "        X", new Vector2(170, 250), Color.Red);
            sb.Draw(_largeBird.Sprite[0].Texture2D[_largeBird.Sprite[0].Itexture2D], new Vector2(380, 220), Color.White);

            sb.DrawString(_normalFont, "Total = " + GlobalVariables.GamePlayer.Mark.ToString() + " gold", new Vector2(160, 400), Color.Blue);

            _dsButton[_iCurrentButton].Draw(gameTime, sb);
            
            _dsButton[_iButtonSaveGame].Draw(gameTime, sb);
        }
    }
}
